package panels;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.*;

import niso.InventoryRecord;
import niso.userFrame;


public class InventoryPane extends JSplitPane implements ActionListener,ItemListener,FocusListener, KeyListener{

	//Attributes
	private userFrame Parent;
	private Date SelectedDate;
	private JButton ViewBt,ManageBt,Nextday,Prevday,NextRows,PrevRows;
	private JTextField Rowbox;
	private ArrayList<InventoryRecord> Supplies;
	private ArrayList<JLabel> SupplyNames;
	private ArrayList<JLabel> Balances;
	private ArrayList<JTextField> InBoxes,OutBoxes;
	private JComboBox DayBox,MonthBox,YearBox;
	private CardLayout CL;
	private JPanel ViewPane;
	private JLabel Statuslbl,DayLbl,MonthLbl,YearLbl,Notify;
	private boolean edit;
	private int batchMark;

	public InventoryPane(userFrame P){
		Parent=P;
		SelectedDate= new Date(); // Set the date to the present
		
		//Initializing buttons
		ViewBt=new JButton("View inventory");
		ManageBt=new JButton("Manage Inventory");

		Nextday=new JButton("Next Day");
		Prevday=new JButton("Previous Day");
		NextRows=new JButton("Next");
		PrevRows=new JButton("Previous");
		Statuslbl=new JLabel("");
		Notify=new JLabel("");	//u can put Notify to the frame to allow it to work in other modules
		
		Rowbox=new JTextField(5);	//text box containing now many rows to show
		DayBox=new JComboBox();		//Combobox containing the  Selected day 
		MonthBox=new JComboBox();	//Combobox containing the  Selected Month
		YearBox=new JComboBox();	//Combobox containing the  Selected Year 
		
		generateUIComponents(Parent.getUser().getusertype());	//Configure UI components
		
		//Add the listeners to the components
		ViewBt.addActionListener(this);
		ManageBt.addActionListener(this);
		DayBox.addItemListener(this);
		MonthBox.addItemListener(this);
		YearBox.addItemListener(this);
		Rowbox.addKeyListener(this);
	}
	
	public void generateUIComponents(String userType){
		GBPane Nav;
		
		// Set navigation panel at the left
		Nav=new GBPane();
		Nav.addComponent(ViewBt, 0, 2, 1, 2, 0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.WEST);
		if(Parent.getUser().getusertype()=="Owner")
			Nav.addComponent(ManageBt, 0, 4, 1, 2, 0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.WEST);
		setLeftComponent(Nav);	// The left side of the split pane will contain the navigation of the module
		
		//Wrong name sorry
		Container ScrlPane=new Container();
		CL=new CardLayout();	//Set values for the card layout
		ScrlPane.setLayout(CL);	//Set the layout of the panel to the card layout
		
		GBPane GBView=new GBPane();
		
		//Add	Components
		GBView.addComponent(Statuslbl,0,0,4,1,0,0,GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		GBView.addComponent(Prevday, 0, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(DayLbl=new JLabel("Day"), 1, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(DayBox, 2, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(MonthLbl=new JLabel("Month"), 3, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(MonthBox, 4, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(YearLbl=new JLabel("Year"), 5, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(YearBox, 6, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Nextday, 7, 1, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		
		GBView.addComponent(PrevRows, 2, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		GBView.addComponent(Rowbox, 3, 2, 2, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		GBView.addComponent(NextRows, 6, 2, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		
		
		Supplies=getDBinventoryItems();
		
		ViewPane=new JPanel();
		
		RecordPaneConfig();
		JScrollPane SP=new JScrollPane(ViewPane);
		SP.setPreferredSize(new Dimension(520,250));
		GBView.addComponent(SP, 0, 4, 8, 3, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
		try{
			setComboBoxItems(MonthBox);
			setComboBoxItems(YearBox);
		} catch(SQLException SqlEx){}
		
		GBView.addComponent(Notify,0,8,4,1,0,0,GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
		
		ScrlPane.add("View Inventory", GBView);
		setRightComponent(ScrlPane);
		Prevday.addActionListener(this);
		Nextday.addActionListener(this);
		PrevRows.addActionListener(this);
		NextRows.addActionListener(this);
		Rowbox.addFocusListener(this);
		ViewBt.setEnabled(false);
		
		
		Statuslbl.setForeground(Color.RED);
	}
	
	public ArrayList<InventoryRecord> getDBinventoryItems(){
		ArrayList<InventoryRecord> Records=new ArrayList<InventoryRecord>();
		ResultSet RS;
		int ctr=0;
		
		RS=Parent.getDBHandler().MySQLSelect("SELECT InventoryID,InventoryIn,InventoryOut,TimeRecorded, unit, productname from Inventory,Product where TimeRecorded=Date(now()) AND prCode=ProductCode;");
		
		try {
			
			while(RS.next()){
				Records.add(new InventoryRecord(RS.getString(1), RS.getString(5),RS.getString(6), RS.getDate(4), RS.getInt(2),RS.getInt(3)));
				ctr++;
			}
			if(ctr==0)
				return InitializeDailyRecord(1);
			RS.close();
		} catch (SQLException SQLe) {
			// TODO Auto-generated catch block
			Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
			Statuslbl.setText("Error "+SQLe);
		}
		
		return Records;
	}
	
	public ArrayList<InventoryRecord> getDBinventoryItems(String s){
		ArrayList<InventoryRecord> Records=new ArrayList<InventoryRecord>();
		ResultSet RS;
		
		/*"SELECT productName,InventoryIn,InventoryOut from Inventory,Product where prCode=ProductCode AND TimeRecorded='"+
		YearBox.getSelectedItem()+"-"+MonthBox.getSelectedItem()+"-"+DayBox.getSelectedItem()+"';"*/
		
		RS=Parent.getDBHandler().MySQLSelect("SELECT InventoryID,InventoryIn,InventoryOut,TimeRecorded, unit, productname from Inventory,Product where TimeRecorded=Date('"+s+"') AND prCode=ProductCode;");
		
		try {
			while(RS.next()){
				Records.add(new InventoryRecord(RS.getString(1), RS.getString(5),RS.getString(6), RS.getDate(4), RS.getInt(2),RS.getInt(3)));
			}
			
			RS.close();
		} catch (SQLException SQLe) {
			// TODO Auto-generated catch block
			Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
			Statuslbl.setText("Error in Collecting Records");
		}
		
		
		return Records;
	}
	
	public void RecordPaneConfig(){
		ViewPane.removeAll();
		//Supplies=getDBinventoryItems();

		if(Supplies.size()>0){
			ViewPane.setLayout(new GridLayout(Supplies.size()+1, 4));
		
			ViewPane.add(new JLabel("Product"));
			ViewPane.add(new JLabel("In"));
			ViewPane.add(new JLabel("Out"));
			ViewPane.add(new JLabel("Balance"));
		
			SupplyNames=new ArrayList<JLabel>();
			InBoxes=new ArrayList<JTextField>();
			OutBoxes=new ArrayList<JTextField>();
			Balances=new ArrayList<JLabel>();
		
			for(int ctr=0;ctr<Supplies.size();ctr++){
				SupplyNames.add(new JLabel(Supplies.get(ctr).getUnitName()+" "+Supplies.get(ctr).getSupplyName()));
				ViewPane.add(SupplyNames.get(ctr));
				InBoxes.add(new JTextField(Supplies.get(ctr).getIn()+""));
				OutBoxes.add(new JTextField(Supplies.get(ctr).getOut()+""));
				Balances.add(new JLabel(""+Supplies.get(ctr).getBalance()));
				ViewPane.add(InBoxes.get(ctr));
				ViewPane.add(OutBoxes.get(ctr));
				ViewPane.add(Balances.get(ctr));
			
				balanceCheck(ctr);
			
				InBoxes.get(ctr).setEditable(false);
				OutBoxes.get(ctr).setEditable(false);
				InBoxes.get(ctr).addFocusListener(this);
				OutBoxes.get(ctr).addFocusListener(this);
				InBoxes.get(ctr).addKeyListener(this);
				OutBoxes.get(ctr).addKeyListener(this);
			}
		}
		else {
			//Statuslbl.setFont(DayLbl.getFont());
			//Statuslbl.setText("No Record for this Day");
			ViewPane.add(new JLabel("<html><h2>No Record on this day</h2></html>"));
		}
		batchMark=Supplies.size();
		Rowbox.setText(Supplies.size()+"");
	}
	
	private void recordPaneConfig(int rows){
		int ctr;
		ViewPane.removeAll();
		//Supplies=getDBinventoryItems();


		ViewPane.setLayout(new GridLayout(rows+1, 4));
		
		ViewPane.add(new JLabel("Product"));
		ViewPane.add(new JLabel("In"));
		ViewPane.add(new JLabel("Out"));
		ViewPane.add(new JLabel("Balance"));
		
		SupplyNames=new ArrayList<JLabel>();
		InBoxes=new ArrayList<JTextField>();
		OutBoxes=new ArrayList<JTextField>();
		Balances=new ArrayList<JLabel>();
		
		for(ctr=0;ctr<Supplies.size();ctr++){
			SupplyNames.add(new JLabel(Supplies.get(ctr).getUnitName()+" "+Supplies.get(ctr).getSupplyName()));
			InBoxes.add(new JTextField(Supplies.get(ctr).getIn()+""));
			OutBoxes.add(new JTextField(Supplies.get(ctr).getOut()+""));
			Balances.add(new JLabel(""+Supplies.get(ctr).getBalance()));
			
		}
		
		for(ctr=0;ctr<rows;ctr++){
			ViewPane.add(SupplyNames.get(ctr));
			ViewPane.add(InBoxes.get(ctr));
			ViewPane.add(OutBoxes.get(ctr));
			ViewPane.add(Balances.get(ctr));
			InBoxes.get(ctr).setEditable(edit);
			OutBoxes.get(ctr).setEditable(edit);
			
			InBoxes.get(ctr).addFocusListener(this);
			OutBoxes.get(ctr).addFocusListener(this);
			balanceCheck(ctr);
			
			InBoxes.get(ctr).setEditable(false);
			OutBoxes.get(ctr).setEditable(false);
			InBoxes.get(ctr).addFocusListener(this);
			OutBoxes.get(ctr).addFocusListener(this);
			InBoxes.get(ctr).addKeyListener(this);
			OutBoxes.get(ctr).addKeyListener(this);
		}
		batchMark=ctr;
	}
	
	
	private void InfoBatches(boolean next){
		int rows=Integer.valueOf(Rowbox.getText()),i=0;
		if(next&&rows<Supplies.size()){
			while(batchMark>=0&&batchMark+rows>Supplies.size())
				batchMark--;
			if(batchMark+rows<=Supplies.size()){
				for(int ctr=batchMark;ctr<rows+batchMark;ctr++){
					SupplyNames.get(i).setIcon(null);
					SupplyNames.get(i).setText(Supplies.get(ctr).getUnitName()+" "+Supplies.get(ctr).getSupplyName());
					InBoxes.get(i).setText(Supplies.get(ctr).getIn()+"");
					OutBoxes.get(i).setText(Supplies.get(ctr).getOut()+"");
					Balances.get(i).setText(Supplies.get(ctr).getBalance()+"");
					balanceCheck(ctr);
					i++;
				}
				batchMark+=rows;

			}
		}
		else if(rows<Supplies.size()){
			batchMark-=rows;
			while(batchMark-rows<0)
				batchMark++;
			if(batchMark-rows>=0){
				int ctr;
				i=rows;
				for(ctr=batchMark;ctr>=batchMark-rows;ctr--){
					SupplyNames.get(i).setIcon(null);
					SupplyNames.get(i).setText(Supplies.get(ctr).getUnitName()+" "+Supplies.get(ctr).getSupplyName());
					InBoxes.get(i).setText(Supplies.get(ctr).getIn()+"");
					OutBoxes.get(i).setText(Supplies.get(ctr).getOut()+"");
					Balances.get(i).setText(Supplies.get(ctr).getBalance()+"");
					balanceCheck(ctr);
					i--;
				}
			}
			
		}
	}
	
	
	public void balanceCheck(int index){
		if(Supplies.get(index).getBalance()<15)
			SupplyNames.get(index).setIcon(Parent.getExclaimImage());
		else SupplyNames.get(index).setIcon(null);
	}
	
	public void setComboBoxItems(JComboBox cb) throws SQLException{
		cb.removeAllItems();
		if(cb==MonthBox){
			cb.addItem("January");cb.addItem("Febuary");cb.addItem("March");
			cb.addItem("April");cb.addItem("May");cb.addItem("June");
			cb.addItem("July");cb.addItem("August");cb.addItem("September");
			cb.addItem("October");cb.addItem("November");cb.addItem("December");
			
			ResultSet RS=Parent.getDBHandler().MySQLSelect("Select Month(now());"),RY,RD;
			RS.first();
			cb.setSelectedIndex(RS.getInt(1)-1);
			
			RY=Parent.getDBHandler().MySQLSelect("Select Year(now());");
			RY.first();
			
			int limit,ctr;
			
			if(RS.getInt(1)==1||RS.getInt(1)==3||RS.getInt(1)==5||RS.getInt(1)==7||RS.getInt(1)==8
					||RS.getInt(1)==10||RS.getInt(1)==12)
				limit=31;
			else if(RS.getInt(1)==2)
				if(RY.getInt(1)%4!=0)
					limit =28;
				else limit=29;
			else limit=30;
			
			for(ctr=1;ctr<=limit;ctr++)
				DayBox.addItem(""+ctr);
			RS.close();
			RY.close();
			
			
			RD=Parent.getDBHandler().MySQLSelect("Select Day(now());");
			RD.first();
			
			DayBox.setSelectedItem(RD.getString(1));
			RD.close();
			
			}
		else if(cb==YearBox){
			ResultSet RS=Parent.getDBHandler().MySQLSelect("Select Year(TimeRecorded) from Inventory group by year(TimeRecorded);");
			
			while(RS.next())
				cb.addItem(RS.getString(1));
			cb.setSelectedIndex(cb.getItemCount()-1);
			RS.close();
		}
		else if(cb==DayBox){
			ResultSet RS=Parent.getDBHandler().MySQLSelect("Select Month(now());"),RY,RD;
			RS.first();
			MonthBox.setSelectedIndex(RS.getInt(1)-1);
			
			RY=Parent.getDBHandler().MySQLSelect("Select Year(now());");
			RY.first();
			
			int limit,ctr;
			
			if(RS.getInt(1)==1||RS.getInt(1)==3||RS.getInt(1)==5||RS.getInt(1)==7||RS.getInt(1)==8
					||RS.getInt(1)==10||RS.getInt(1)==12)
				limit=31;
			else if(RS.getInt(1)==2)
				if(RY.getInt(1)%4!=0)
					limit =28;
				else limit=29;
			else limit=30;
			
			for(ctr=1;ctr<=limit;ctr++)
				DayBox.addItem(""+ctr);
			RS.close();
			RY.close();
			
			
			RD=Parent.getDBHandler().MySQLSelect("Select Day(now());");
			RD.first();
			
			DayBox.setSelectedItem(RD.getString(1));
			RD.close();
		}
	}
	
	
	private ArrayList<InventoryRecord> InitializeDailyRecord(int i) throws SQLException{
		ArrayList<InventoryRecord> Records=new ArrayList<InventoryRecord>();
		ResultSet RS,RT;
		int ctr=0;
		
		RS=Parent.getDBHandler().MySQLSelect("SELECT InventoryID,InventoryIn,InventoryOut, unit, productname,prCode from Inventory,Product where TimeRecorded=Date_SUB(CurDate(),INTERVAL "+i+" Day)AND prCode=ProductCode;");
		RT=Parent.getDBHandler().MySQLSelect("SELECT curDate();");
		RT.first();
		while(RS.next()){
			Records.add(new InventoryRecord(RT.getString(1)+ctr, RS.getString(4), RS.getString(5), RT.getDate(1), RS.getInt(2)-RS.getInt(3), 0));
			Parent.getDBHandler().MySQLinsert("Insert INTO INventory values ('"+(RT.getString(1)+ctr)
					+"',"+(RS.getInt(2)-RS.getInt(3))+",0,now(),'"+RS.getString(6)+"');");
			ctr++;
			
		}
		
		if(ctr==0)
			InitializeDailyRecord(i+1);
		
		RT.close();
		RS.close();
		 return Records;
	}
	
	private void manageVisibility(){
		Prevday.setVisible(!edit);
		Nextday.setVisible(!edit);
		MonthLbl.setVisible(!edit);
		YearLbl.setVisible(!edit);
		DayBox.setVisible(!edit);
		MonthBox.setVisible(!edit);
		YearBox.setVisible(!edit);
		DayLbl.setVisible(!edit);
		
		for(int ctr=0;ctr<Supplies.size();ctr++){
			InBoxes.get(ctr).setEditable(edit);
			OutBoxes.get(ctr).setEditable(edit);
		}
	}
	
	
	public void RowChage(int rows){
		Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
		if(rows>Supplies.size()){
			rows=Supplies.size();
			Statuslbl.setText("Rows exceed number of records");
		}
		if(rows<0){
			rows=0;
			Statuslbl.setText("Must have at least 0 row");
		}
		Rowbox.setText(""+rows);
		recordPaneConfig(rows);
		paintAnsestors(ViewPane);
		manageVisibility();
	}
	
	
	
	//method that changes the DayBox based on the month
		public void DayBoxConfig(int month){
			int limit,year=Integer.valueOf(YearBox.getSelectedItem().toString()),Day=DayBox.getItemCount();
			//Determine month to determine number of days
			if(month==0||month==2||month==4||month==6||month==7||month==9||month==11)
				limit =31;
			else if(month==1)
				if(year%4!=0)
					limit =28;
				else limit=29;
			else limit=30;
			
			DayBox.removeItemListener(this);
			//DayBox.setSelectedIndex(0);
			
			if(Day<limit)
				for(int ctr=Day+1;ctr<=limit;ctr++)
					DayBox.addItem(""+ctr);
			else if(Day>limit){
				DayBox.removeAllItems();
				for(int ctr=1;ctr<=limit;ctr++)
					DayBox.addItem(""+ctr);
			}
			MonthBox.addItemListener(this);
			DayBox.addItemListener(this);
		}
		
		//Method that responds to the next and previous day buttons
		public void NextPrevDay(int change){
			int month;
			if(change>0){
				if(DayBox.getSelectedIndex()<DayBox.getItemCount()-change)
					DayBox.setSelectedIndex(DayBox.getSelectedIndex()+change);
				else if(MonthBox.getSelectedIndex()<MonthBox.getItemCount()-change){
					MonthBox.removeItemListener(this);
					MonthBox.setSelectedIndex(MonthBox.getSelectedIndex()+change);
					month=MonthBox.getSelectedIndex();
					
					DayBoxConfig(month);
					DayBox.setSelectedIndex(0);
				}
				else{
					YearBox.removeItemListener(this);
					YearBox.addItem(""+(Integer.valueOf(YearBox.getSelectedItem().toString())+1));
					YearBox.setSelectedIndex(YearBox.getItemCount()-1);
					MonthBox.setSelectedIndex(0);
					DayBox.setSelectedIndex(0);
					YearBox.addItemListener(this);
					
				}
				
			}
			else if(change<0){
				if(DayBox.getSelectedIndex()>0)
					DayBox.setSelectedIndex(DayBox.getSelectedIndex()+change);
				else if(MonthBox.getSelectedIndex()>0){
					MonthBox.removeItemListener(this);
					MonthBox.setSelectedIndex(MonthBox.getSelectedIndex()+change);
					month=MonthBox.getSelectedIndex();
					DayBoxConfig(month);
					
					DayBox.setSelectedIndex(DayBox.getItemCount()-1);
				}
				else if(YearBox.getSelectedIndex()>0)
					YearBox.setSelectedIndex(YearBox.getSelectedIndex()+change);
				else Statuslbl.setText("No more record in the previous Years");
			}
		}
	
	
	
	@Override
	public void actionPerformed(ActionEvent AE) {
		
		if(AE.getSource()==ManageBt){
			edit=true;
			Supplies=getDBinventoryItems();
			RecordPaneConfig();
			Notify.setText("Can only modify records of today");
		}
			
		else if(AE.getSource()==ViewBt)
			edit=false;
		else if(AE.getSource()==Nextday)
			NextPrevDay(1);
		else if(AE.getSource()==Prevday)
			NextPrevDay(-1);
		else if(AE.getSource()==NextRows)
			InfoBatches(true);
		else if(AE.getSource()==PrevRows)
			InfoBatches(false);
		
		manageVisibility();
		
		Statuslbl.setVisible(edit);
		ManageBt.setEnabled(!edit);
		ViewBt.setEnabled(edit);
	}

	public void paintAnsestors(Container cn){
		Container C=cn;
		while(C.getParent()!=null){
			C.repaint();
			C=C.getParent();
		}
	}
	
	@Override
	public void itemStateChanged(ItemEvent IE) {
		Notify.setText("");
		
		if(IE.getSource()==MonthBox){
			MonthBox.removeItemListener(this);//Remove recursion since itemstateChanged can be triggered in this method
			DayBoxConfig(MonthBox.getSelectedIndex());
		}
		if(IE.getSource()==DayBox||IE.getSource()==MonthBox||IE.getSource()==YearBox){
			Supplies=getDBinventoryItems(YearBox.getSelectedItem().toString()+"-"+(MonthBox.getSelectedIndex()+1)+"-"+DayBox.getSelectedItem().toString());
			
			RecordPaneConfig();
			Notify.setText("Now viewing records on "+MonthBox.getSelectedItem()+" "+DayBox.getSelectedItem()+" "+YearBox.getSelectedItem().toString());
			paintAnsestors(ViewPane);
		}
	}

	@Override
	public void focusGained(FocusEvent FE) {
		
		
	}

	@Override
	public void focusLost(FocusEvent FE) {
		int i;
		Statuslbl.setText("");
		Notify.setText("");
			if(InBoxes.contains(FE.getSource())){
				i=InBoxes.indexOf(FE.getSource());
				Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
				
				try{
					if(Integer.valueOf(InBoxes.get(i).getText())>=0&&Integer.valueOf(InBoxes.get(i).getText())>=Integer.valueOf(OutBoxes.get(i).getText()))
					Parent.getDBHandler().MySQLUpdate("UPDATE Inventory SET InventoryIn='"+InBoxes.get(i).getText()+"' where InventoryID='"
						+Supplies.get(i).getID()+"';");
					else {
						if(Integer.valueOf(InBoxes.get(i).getText())<0)
							Statuslbl.setText("Please Enter non negative values");
						else Statuslbl.setText("Out can not be more than In");
						InBoxes.get(i).setText(Supplies.get(i).getIn()+"");
					}
					if(Integer.valueOf(InBoxes.get(i).getText())!=Supplies.get(i).getIn())
						Notify.setText("Changes for "+Supplies.get(i).getSupplyName()+" are saved");
					Supplies.get(i).setIn(Integer.valueOf(InBoxes.get(i).getText()));
					Balances.get(i).setText(Supplies.get(i).getBalance()+"");
					balanceCheck(i);
					
					Supplies.get(i).setIn(Integer.valueOf(InBoxes.get(i).getText()));
					}catch(Exception general){
						InBoxes.get(i).setText(Supplies.get(i).getIn()+"");
						Statuslbl.setText("Error: Not a number");
					}
				
		}
			else if(OutBoxes.contains(FE.getSource())){
				i=OutBoxes.indexOf(FE.getSource());
				try{
					if(Integer.valueOf(OutBoxes.get(i).getText()) <=Integer.valueOf(InBoxes.get(i).getText())&&Integer.valueOf(OutBoxes.get(i).getText())>=0){
					Parent.getDBHandler().MySQLUpdate("UPDATE Inventory SET InventoryOut='"+OutBoxes.get(i).getText()+"' where InventoryID='"
						+Supplies.get(i).getID()+"';");
					if(Integer.valueOf(OutBoxes.get(i).getText()) !=Supplies.get(i).getOut())
						Notify.setText("Changes for "+Supplies.get(i).getSupplyName()+" are saved");
					Supplies.get(i).setOut(Integer.valueOf(OutBoxes.get(i).getText()));
					Balances.get(i).setText(Supplies.get(i).getBalance()+"");
					balanceCheck(i);
					
					}
					else{
						Statuslbl.setForeground(Color.RED);
						if(Integer.valueOf(OutBoxes.get(i).getText()) >Integer.valueOf(InBoxes.get(i).getText()))
							Statuslbl.setText("Out can not be more than In");
						else Statuslbl.setText("Please Enter non negative values");
						OutBoxes.get(i).setText(Supplies.get(i).getOut()+"");
					}
				}catch(Exception general){
					OutBoxes.get(i).setText(Supplies.get(i).getOut()+"");
					Statuslbl.setForeground(Color.RED);
					Statuslbl.setText("Error: Not a number");
				}
				
				paintAnsestors(ViewPane);
		}
			else if(FE.getSource()==Rowbox){
				try{
					RowChage(Integer.valueOf(Rowbox.getText()));
				}catch(NumberFormatException NF){
					Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
					Statuslbl.setText("Please enter a valid number");
				}
			}
	
	}

	@Override
	public void keyPressed(KeyEvent KE) {
		
		Statuslbl.setFont(Parent.getWarningFont(Statuslbl));
	}

	@Override
	public void keyReleased(KeyEvent KE) {
		// TODO Auto-generated method stub
		
		int i;
		Notify.setText("");
		if(KE.getKeyCode()==KeyEvent.VK_QUOTE)
			Statuslbl.setText("Invalid");
		else Statuslbl.setText("");
		
		if(KE.getSource()==Rowbox&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			try{
				RowChage(Integer.valueOf(Rowbox.getText()));
				//Rowbox.getParent().requestFocus();
			}catch(NumberFormatException NF){

				Statuslbl.setText("Please enter a valid number");
			}
			paintAnsestors(ViewPane);
		}
		else if(InBoxes.contains(KE.getSource())&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			i=InBoxes.indexOf(KE.getSource());
			OutBoxes.get(i).requestFocus();
		}
		else if(OutBoxes.contains(KE.getSource())&&KE.getKeyCode()==KeyEvent.VK_ENTER){
			i=OutBoxes.indexOf(KE.getSource());
			if(i<InBoxes.size()-1)
				InBoxes.get(i+1).requestFocus();
		}
	}

	@Override
	public void keyTyped(KeyEvent KE) {
		// TODO Auto-generated method stub
		
	}
}